<?php



function str_random($len = 6) {
    $s = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
    mt_srand((double) microtime() * 1000000);
    $str = '';
    for ($i = 0; $i < $len; $i++)
        $str .= substr($s, (mt_rand() % (strlen($s))), 1);
    return strtolower($str);
}

function api_cut_words($str, $length, $word_cut = '', $charset = 'UTF-8') {
    $str = stripslashes(strip_tags($str));
    $str = html_entity_decode($str, ENT_QUOTES, $charset);
    if (mb_strlen($str, $charset) > $length) {
        $arr = explode(' ', $str);
        $str = mb_substr($str, 0, $length, $charset);
        $arrRes = explode(' ', $str);
        $last = $arr[count($arrRes) - 1];
        unset($arr);
        if (strcasecmp($arrRes[count($arrRes) - 1], $last)) {
            unset($arrRes[count($arrRes) - 1]);
        }
        return implode(' ', $arrRes) . $word_cut;
    }
    return $str . $word_cut;
}

function api_format_date($created, $format = 'hm') {
    $year = format_date($created, 'custom', 'Y');
    $month = format_date($created, 'custom', 'm');
    $day = format_date($created, 'custom', 'd');
    $hour = format_date($created, 'custom', 'H');
    $minute = format_date($created, 'custom', 'i');

    if ($format == 'd-m-Y') {
        $date = $day . '-' . $month . '-' . $year;
    } elseif ($format == 'd/m/Y') {
        $date = $day . '/' . $month . '/' . $year;
    } elseif ($format == 'full') {
        $date = $hour . ' giờ ' . $minute . ' phút, ' . $day . '/' . $month . '/' . $year;
    } elseif ($format == 'hm') {
        $date = $hour . ' giờ ' . $minute . ' phút ';
    }

    return $date;
}


function str_utf8($str) {
    $chars = array(
        'a' => array('ấ', 'ầ', 'ẩ', 'ẫ', 'ậ', 'Ấ', 'Ầ', 'Ẩ', 'Ẫ', 'Ậ', 'ắ', 'ằ', 'ẳ', 'ẵ', 'ặ', 'Ắ', 'Ằ', 'Ẳ', 'Ẵ', 'Ặ', 'á', 'à', 'ả', 'ã', 'ạ', 'â', 'ă', 'Á', 'À', 'Ả', 'Ã', 'Ạ', 'Â', 'Ă'),
        'e' => array('ế', 'ề', 'ể', 'ễ', 'ệ', 'Ế', 'Ề', 'Ể', 'Ễ', 'Ệ', 'é', 'è', 'ẻ', 'ẽ', 'ẹ', 'ê', 'É', 'È', 'Ẻ', 'Ẽ', 'Ẹ', 'Ê'),
        'i' => array('í', 'ì', 'ỉ', 'ĩ', 'ị', 'Í', 'Ì', 'Ỉ', 'Ĩ', 'Ị'),
        'o' => array('ố', 'ồ', 'ổ', 'ỗ', 'ộ', 'Ố', 'Ồ', 'Ổ', 'Ô', 'Ộ', 'ớ', 'ờ', 'ở', 'ỡ', 'ợ', 'Ớ', 'Ờ', 'Ở', 'Ỡ', 'Ợ', 'ó', 'ò', 'ỏ', 'õ', 'ọ', 'ô', 'ơ', 'Ó', 'Ò', 'Ỏ', 'Õ', 'Ọ', 'Ô', 'Ơ'),
        'u' => array('ứ', 'ừ', 'ử', 'ữ', 'ự', 'Ứ', 'Ừ', 'Ử', 'Ữ', 'Ự', 'ú', 'ù', 'ủ', 'ũ', 'ụ', 'ư', 'Ú', 'Ù', 'Ủ', 'Ũ', 'Ụ', 'Ư'),
        'y' => array('ý', 'ỳ', 'ỷ', 'ỹ', 'ỵ', 'Ý', 'Ỳ', 'Ỷ', 'Ỹ', 'Ỵ'),
        'd' => array('đ', 'Đ'),
        '' => array("'", '"', '+', '%', '$', '#', '@', '&', '*'),
        '-' => array(' ', '---', '?', '!', ',', '/')
    );
    foreach ($chars as $key => $arr)
        foreach ($arr as $val)
            $str = str_replace($val, $key, $str);
    return strtolower($str);
}

function addMetaTag($name , $content){
    $meta =  array(
            '#type' => 'html_tag',
            '#tag' => 'meta',
            '#attributes' => array(                
                'name' => $name, 
                'content' => $content ,
            )
    );  
    drupal_add_html_head( $meta, 'meta_' . $name );
}
 
function addOgMetaTag($name , $content){
    $meta =  array(
            '#type' => 'html_tag',
            '#tag' => 'meta',
            '#attributes' => array(                
                'property' => 'og:' . $name, 
                'content' => $content ,
            )
    );  
    drupal_add_html_head( $meta, 'meta_og_' . $name );
}










